<?php
/**
 * Created by PhpStorm.
 * User: iwm
 * Date: 2017/1/10
 * Time: 18:50
 */

namespace app\api\model\page\user;

use app\api\model\dao\UserAccount;
use think\Validate;
use app\library\PageBase;
use think\Log;
use app\library\Error;

class IsRegisted extends PageBase
{
    private $objUserAccount;

    public function __construct()
    {
        parent::__construct();
        $this->objUserAccount = new UserAccount();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'identity_type' => 'require',
            'identifier' => 'require',
        ];
        $msg = [
            'identity_type.require' => '账号类型必填',
            'identifier.require' => '账号必填',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }


    public function excute($param)
    {
        // 1、参数校验
        $ret = $this->checkParam($param);
        if ($ret) {
            Log::alert(__METHOD__ . ' param: ' . json_encode($param));
            return $this->getRes(Error::ERR_PARAM);
        }
        $ret = $this->objUserAccount->getUserAccountByPhone($param['identifier']);
        if (count($ret) != 0) {
            return $this->getRes(Error::ERR_REGISTERED);
        }
        return $this->getRes();
    }
}